# 定义项目
PROJECTS := api mq cron

# 默认目标是显示帮助信息
.DEFAULT_GOAL := help

# 输出目录
OUTPUT_DIR := ./bin

# upx 压缩率 1~9
UPX_LEVEL ?= 0

.PHONY: all clean help $(PROJECTS)

# 编译所有项目
all: $(PROJECTS)

# 编译指定项目
$(PROJECTS): % :
	@echo "building $@..."
	@go build -ldflags="-s -w" -o $(OUTPUT_DIR)/$@.bin ./service/api/cmd/$@;
	@echo "$@.bin has built succ"
	@if [ $(UPX_LEVEL) -gt 0 ]; then \
        echo "compressing $@.bin with UPX level $(UPX_LEVEL)..."; \
        upx -$(UPX_LEVEL) $(OUTPUT_DIR)/$@.bin; \
    else \
        echo "not compressing by UPX, pass"; \
    fi

# 清理可执行文件
clean:
	@echo "clean up all bin files ..."
	@for project in $(PROJECTS); do \
      	echo "clean $$project ..."; \
      	rm -rf $(OUTPUT_DIR)/$$project.bin; \
    done
	@echo "all bin files has clean"

# 打印帮助信息
help:
	@echo "available targets:"
	@echo "  all	- build all projects"
	@for project in $(PROJECTS); do \
		echo "  $$project	- build $$project"; \
	done
	@echo "  clean	- clean up all bin files"
	@echo "  help	- display this help message"